# -*- coding: utf-8 -*-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import config
# from flask_wtf.csrf import CsrfProtect

# csrf = CsrfProtect()

db = SQLAlchemy()
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.blueprint_login_views = 'auth.login'


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.init_app(app)
    login_manager.init_app(app)
    # api.init_app(app)
    # csrf.init_app(app)

    from cmdb.auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from cmdb.main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from cmdb.resources import api_bp
    app.register_blueprint(api_bp, url_prefix='/api')

    # from cmdb.resources import resource_views
    # app.register_blueprint(resource_views)

    return app
